﻿using GeometryMod.Models;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Windows;

namespace GeometryMod.ViewModels
{
	public class GeometryMainViewModel : BindableBase
    {
        public string Title { get; set; } = "几何模块";

        private string pointsArray = "";
        public string PointsArray
        {
            get { return pointsArray; }
            set { SetProperty(ref pointsArray, value); }
        }

        private string points4Array = "";
        public string Points4Array
        {
            get
            {
                return points4Array;
            }
            set
            {
                SetProperty(ref points4Array, value);
            }
        }


        private string resultTB = "";
        public string ResultTB
        {
            get { return resultTB; }
            set { SetProperty(ref resultTB, value); }
        }


        public DelegateCommand Check4PointCoplanarCommand { get; set; }

        public DelegateCommand Js3PointPlantCommand { get; set; }

        public DelegateCommand TwoLineJDCommand { get; set; }

        public GeometryMainViewModel()
        {
            Js3PointPlantCommand = new DelegateCommand(OnJs3PointPlant);
            Check4PointCoplanarCommand = new DelegateCommand(OnCheck4PointCoplanar);
            TwoLineJDCommand = new DelegateCommand(OnTwoLineJD);
        }

        private void OnTwoLineJD()
        {
            //Points4Array
            if (!string.IsNullOrEmpty(Points4Array))
            {
                var _tempPointArray = Points4Array.Trim().Replace(",", " ");
                var pointList = _tempPointArray.Split(' ');
                if (pointList.Length == 12)
                {
                    var d1x = 0.0;
                    var d1y = 0.0;
                    var d1z = 0.0;

                    var d2x = 0.0;
                    var d2y = 0.0;
                    var d2z = 0.0;

                    var d3x = 0.0;
                    var d3y = 0.0;
                    var d3z = 0.0;

                    var d4x = 0.0;
                    var d4y = 0.0;
                    var d4z = 0.0;

                    double.TryParse(pointList[0], out d1x);
                    double.TryParse(pointList[1], out d1y);
                    double.TryParse(pointList[2], out d1z);

                    double.TryParse(pointList[3], out d2x);
                    double.TryParse(pointList[4], out d2y);
                    double.TryParse(pointList[5], out d2z);

                    double.TryParse(pointList[6], out d3x);
                    double.TryParse(pointList[7], out d3y);
                    double.TryParse(pointList[8], out d3z);

                    double.TryParse(pointList[9], out d4x);
                    double.TryParse(pointList[10], out d4y);
                    double.TryParse(pointList[11], out d4z);


                    Point3D v1 = new Point3D(d1x, d1y, d1z);
                    Point3D v2 = new Point3D(d2x, d2y, d2z);
                    Point3D v3 = new Point3D(d3x, d3y, d3z);
                    Point3D v4 = new Point3D(d4x, d4y, d4z);
                    Point3D resultPoint = new Point3D();

                    try
                    {
                        var result = Tools.GetIntersection(v1, v2, v3, v4, out resultPoint);
                        if (result == false)
                        {

                        }
                        else
                        {
                            MessageBox.Show("这四点共面");
                        }

                    }
                    catch (ArgumentException ex)
                    {
                        MessageBox.Show($"Error: {ex.Message}");
                    }
                }
            }

        }

        private void OnCheck4PointCoplanar()
        {
            if (!string.IsNullOrEmpty(PointsArray))
            {
                var _tempPointArray = PointsArray.Trim().Replace(",", " ");
                var pointList = _tempPointArray.Split(' ');
                if (pointList.Length == 12)
                {
                    var d1x = 0.0;
                    var d1y = 0.0;
                    var d1z = 0.0;

                    var d2x = 0.0;
                    var d2y = 0.0;
                    var d2z = 0.0;

                    var d3x = 0.0;
                    var d3y = 0.0;
                    var d3z = 0.0;

                    var d4x = 0.0;
                    var d4y = 0.0;
                    var d4z = 0.0;

                    double.TryParse(pointList[0], out d1x);
                    double.TryParse(pointList[1], out d1y);
                    double.TryParse(pointList[2], out d1z);

                    double.TryParse(pointList[3], out d2x);
                    double.TryParse(pointList[4], out d2y);
                    double.TryParse(pointList[5], out d2z);

                    double.TryParse(pointList[6], out d3x);
                    double.TryParse(pointList[7], out d3y);
                    double.TryParse(pointList[8], out d3z);

                    double.TryParse(pointList[9], out d4x);
                    double.TryParse(pointList[10], out d4y);
                    double.TryParse(pointList[11], out d4z);


                    Point3D v1 = new Point3D(d1x, d1y, d1z);
                    Point3D v2 = new Point3D(d2x, d2y, d2z);
                    Point3D v3 = new Point3D(d3x, d3y, d3z);
                    Point3D v4 = new Point3D(d4x, d4y, d4z);

                    var result = Tools.ArePointsCoplanar(v1, v2, v3, v4);
                    if (result == false)
                    {

                    }
                    else
                    {
                        MessageBox.Show("这四点共面");
                    }
                }
            }
        }

        private void OnJs3PointPlant()
        {
            if (!string.IsNullOrEmpty(PointsArray)) 
            {
                var _tempPointArray = PointsArray.Trim().Replace(",", " ");
                var pointList = _tempPointArray.Split(' ');
                if (pointList.Length ==9) 
                {
                    var d1x = 0.0;
                    var d1y = 0.0;
                    var d1z = 0.0;

                    var d2x = 0.0;
                    var d2y = 0.0;
                    var d2z = 0.0;

                    var d3x = 0.0;
                    var d3y = 0.0;
                    var d3z = 0.0;

                    double.TryParse(pointList[0], out d1x);
                    double.TryParse(pointList[1], out d1y);
                    double.TryParse(pointList[2], out d1z);

                    double.TryParse(pointList[3], out d2x);
                    double.TryParse(pointList[4], out d2y);
                    double.TryParse(pointList[5], out d2z);

                    double.TryParse(pointList[6], out d3x);
                    double.TryParse(pointList[7], out d3y);
                    double.TryParse(pointList[8], out d3z);

                    Point3D p1 = new Point3D(d1x, d1y, d1z);
                    Point3D p2 = new Point3D(d2x, d2y, d2z);
                    Point3D p3 = new Point3D(d3x, d3y, d3z);

                    var result =  PlaneEquation.Calculate(p1,p2,p3);
                    ResultTB = $"({result.A})·X + ({result.B})·Y + ({result.C})·Z + ({result.D}) = 0";
                }
            }
        }
    }
}
